from web.models import Data,Out_GameTokens,Out_PhoneCharge
from web import models


def phone_data_out(fee,consumer,
                   admin_name,in_storage_of_admin_name,out_storage_of_admin_name,consumer_of_admin_name,
                   out_storage_object,

                   ):

#这行代码的意思是：
# - `Data.objects.get_or_create(name=consumer)`：这是 Django ORM 的一个方法，用来从数据库中获取一条 `Data` 模型的记录。
# -  如果数据库中已经存在 `name=consumer` 的记录，则返回该记录；如果不存在，则会创建一条新的记录，并将 `name` 字段的值设为 `consumer`。
# - `data_instance_consumer, created`：这是对 `get_or_create` 返回结果的解包。
# - `data_instance_consumer`：是返回的 `Data` 对象实例，无论是从数据库中查找到的现有记录，还是新创建的记录。
# - `created`：是一个布尔值，指示是否创建了新的记录。如果数据库中已经有该 `name` 的记录，则 `created` 为 `False`；如果没有，则为 `True`。
# 总的来说，这行代码尝试查找 `name=consumer` 的 `Data` 记录，如果没有找到，则创建一条新的记录。
    data_instance_consumer, created = Data.objects.get_or_create(name=consumer)

#    data_instance_consumer.out_phonecharge = models.Out_PhoneCharge.objects.filter(consumer=consumer).first()
    data_instance_consumer.name = consumer
    data_instance_consumer.total_all_fee +=  fee
    data_instance_consumer.total_all_order_num += 1
    data_instance_consumer.total_phone_num += 1
    data_instance_consumer.total_phone_fee += fee
    data_instance_consumer.system_fee += 1
    data_instance_consumer.save()


    data_instance_outperson,created = Data.objects.get_or_create(name=out_storage_object.name)
    print('吴佩琦')

    data_instance_outperson.name = out_storage_object.name
    data_instance_outperson.total_all_fee += fee
    data_instance_outperson.total_all_order_num += 1
    data_instance_outperson.total_phone_num += 1
    data_instance_outperson.total_phone_fee += fee
    data_instance_outperson.system_fee += 1
    #1.入库人和登录用户的直属管理一致 且 出库人也在此圈子
    if in_storage_of_admin_name == admin_name: #and out_storage_of_admin_name == admin_name:
        print('操')
        data_instance_outperson.save()

    if in_storage_of_admin_name != admin_name: #and out_storage_of_admin_name != admin_name:
        print('hello 哈哈哈哈哈哈')
        data_instance_outperson.currency += fee
        data_instance_outperson.save()



    


def phone_data_reout(fee,consumer,in_person,out_person):
    data_instance_consumer, created = Data.objects.get_or_create(name=consumer)

    data_instance_consumer.out_phonecharge = models.Out_PhoneCharge.objects.filter(consumer=consumer).first()
    data_instance_consumer.name = consumer
    data_instance_consumer.total_all_fee -= fee
    data_instance_consumer.total_all_order_num -= 1
    data_instance_consumer.total_phone_num -= 1
    data_instance_consumer.total_phone_fee -= fee
    data_instance_consumer.system_fee -= 1
    data_instance_consumer.save()

def game_data_out(fee,consumer,in_person,out_person):
    data_instance_consumer, created = Data.objects.get_or_create(name=consumer)

    data_instance_consumer.name = consumer
    data_instance_consumer.total_all_fee += fee
    data_instance_consumer.total_all_order_num += 1
    data_instance_consumer.total_game_num += 1
    data_instance_consumer.total_game_fee += fee
    data_instance_consumer.system_fee += 1
    data_instance_consumer.save()


def game_data_reout(fee,consumer,in_person,out_person):
    data_instance_consumer, created = Data.objects.get_or_create(name=consumer)

    data_instance_consumer.name = consumer
    data_instance_consumer.total_all_fee -= fee
    data_instance_consumer.total_all_order_num -= 1
    data_instance_consumer.total_game_num -= 1
    data_instance_consumer.total_game_fee -= fee
    data_instance_consumer.system_fee -= 1
    data_instance_consumer.save()